package com.dehui.traders.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.RealtimeQuote;

import org.w3c.dom.Text;

import me.goldze.mvvmhabit.utils.Utils;

public class PanKInfoView extends PopupWindow{

    private final View contentView;
    private Activity context;
    private RealtimeQuote mRealTimeQuote;

    public PanKInfoView(Activity context, RealtimeQuote mRealTimeQuote){
        this.context = context;
        this.mRealTimeQuote = mRealTimeQuote;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popup_pank_info, null);
        this.setContentView(contentView);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = context.getWindow().getAttributes();
                params.alpha = 1f;
                context.getWindow().setAttributes(params);
            }
        });

        initViews();

    }

    private void initViews() {
        contentView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PanKInfoView.this.dismiss();
            }
        });
        TextView tvJinKai = contentView.findViewById(R.id.tv_jinkai);
        TextView tvZongShiZhi = contentView.findViewById(R.id.tv_zongshizhi);
        TextView tvZuoShou = contentView.findViewById(R.id.tv_zuoshou);
        TextView tvLiuTongZhi = contentView.findViewById(R.id.tv_liutongzhi);
        TextView tvHeight = contentView.findViewById(R.id.tv_height);
        TextView tvShiYingLv = contentView.findViewById(R.id.tv_shiyinglv);
        TextView tvLow = contentView.findViewById(R.id.tv_low);
        TextView tvShiJingLv = contentView.findViewById(R.id.tv_shijinglv);
        TextView tvChangJiaoLiang = contentView.findViewById(R.id.tv_changjiaoliang);
        TextView tvZhengFu = contentView.findViewById(R.id.tv_zhengfu);
        TextView tvChengjiaoE = contentView.findViewById(R.id.tv_changjiaoe);
        TextView tvHuanShouLv = contentView.findViewById(R.id.tv_huanshoulv);

        if (mRealTimeQuote == null)
            return;
        tvJinKai.setText(Utils.doubleToString(mRealTimeQuote.openPrice));
        tvZuoShou.setText(Utils.doubleToString(mRealTimeQuote.closePrice));
        tvHeight.setText(Utils.doubleToString(mRealTimeQuote.highPrice));
        tvLow.setText(Utils.doubleToString(mRealTimeQuote.lowPrice));
        tvChangJiaoLiang.setText(Utils.doubleToString(Double.valueOf(mRealTimeQuote.volume) / 1000000) + "万");
        tvChengjiaoE.setText(Utils.doubleToString(Double.valueOf(mRealTimeQuote.volume) / 1000000000) + "亿");

        int red = context.getResources().getColor(R.color.red);
        int green = context.getResources().getColor(R.color.green);

        if (mRealTimeQuote.price - mRealTimeQuote.openPrice > 0){
            tvJinKai.setTextColor(red);
            tvHeight.setTextColor(red);
            tvLow.setTextColor(red);
        }else if (mRealTimeQuote.price - mRealTimeQuote.openPrice < 0){
            tvJinKai.setTextColor(green);
            tvHeight.setTextColor(green);
            tvLow.setTextColor(green);
        }else {
            tvJinKai.setTextColor(Color.BLACK);
            tvHeight.setTextColor(Color.BLACK);
            tvLow.setTextColor(Color.BLACK);
        }

    }

    public void show(){
        this.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.alpha = 0.5f;
        context.getWindow().setAttributes(params);

    }

}
